﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using DotNetCore.CAP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Application.IServices;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.DTO;
using W.M.Materiel.Infrastruecure.IRepository;
using W.M.Materiel.Infrastruecure.Repository;

namespace W.M.Materiel.Application.Services
{
    public class Bill_of_materialsServices : BaseServices<bill_of_materials, String>, IBill_of_materialsServices
    {
        private readonly IBill_of_materialsRepository bill_Of_MaterialsRepository;
        private readonly ICapPublisher capPublisher;

        public Bill_of_materialsServices(IBill_of_materialsRepository bill_Of_MaterialsRepository, ICapPublisher capPublisher) : base(bill_Of_MaterialsRepository)
        {
            this.bill_Of_MaterialsRepository = bill_Of_MaterialsRepository;
            this.capPublisher = capPublisher;
        }

        /// <summary>
        /// 物料信息+查询+分页
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件：物料名称</param>
        /// <param name="id">查询条件：单位id</param>
        /// <returns>物料信息</returns>
        public List<Bill_of_materialsDTO> GetBill_Of_Materials(int pageSize, int pageIndex, string? name, string? id,out int totalCount,out int pageCount)
        {
            return bill_Of_MaterialsRepository.GetBill_Of_Materials( pageSize,pageIndex,name,id,out totalCount,out pageCount);    
        }

        

        /// <summary>
        /// 反填BOM配件信息
        /// </summary>
        /// <param name="material_code">物料编号</param>
        /// <returns>BOM配件数据</returns>
        public async Task<bill_of_materials> Getbackfill(string? material_code)
        {
            Expression<Func<bill_of_materials, bool>> wherename = x => x.material_code == material_code;
            var list = await bill_Of_MaterialsRepository.GetShowObjAsync(wherename);

            return list;
        }

        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <param name="c">物料实体</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> GetBill_Of_MaterialAdd(bill_of_materials c)
        {
            ApiReturn<string> apiReturn =new ApiReturn<string>();
            var list=await bill_Of_MaterialsRepository.AddInfor_Async(c);
            if (list > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "添加成功!";

                //添加成功需要传一个消息队列  传递哪个模块 还有编号
                string numberName = "物料系统-" + c.material_code;
                capPublisher.Publish("GetNewNumber", numberName);
                bill_Of_MaterialsRepository.CommitTranInfor();
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "添加失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 删除物料信息
        /// </summary>
        /// <param name="c">物料编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<int>> GetDelBill(string? c)
        {
            ApiReturn<int> apiReturn =new ApiReturn<int>();
            var res =await bill_Of_MaterialsRepository.GetShowObjAsync(x => x.material_code == c);
            var list =await bill_Of_MaterialsRepository.DelInfor_Async(res);
            if (list != null)
            {
                apiReturn.Code=200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }

            return apiReturn;
        }

        /// <summary>
        /// 修改物料信息
        /// </summary>
        /// <param name="c">物料信息实体</param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> GetUpdateBillInfo(bill_of_materials c)
        {
            ApiReturn<string> apiReturn =new ApiReturn<string>();
            var res =await bill_Of_MaterialsRepository.GetShowInforAsync(x => x.material_code.Equals(c.material_code));
            foreach (var item in res)
            {
                item.material_code = c.material_code;
                item.material_name = c.material_name;
                item.specification_model = c.specification_model;
                item.brand = c.brand;
                item.unit_id = c.unit_id;
                item.serial_number = c.serial_number;
                item.material_attribute = c.material_attribute;
                item.purchase_price = c.purchase_price;
                item.minimum_inventory = c.minimum_inventory;
                item.maximum_inventory = c.maximum_inventory;
                item.material_life = c.material_life;
                item.alarm_days = c.alarm_days;
                item.status = c.status;
                item.remark = c.remark;
                item.auxiliary_unit = c.auxiliary_unit;
                item.picture = c.picture;
                item.attachments = c.attachments;
            }
            var list =await bill_Of_MaterialsRepository.UpdInfor_Batch_Async(res);
            if (list > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "修改失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 反填物料信息
        /// </summary>
        /// <param name="bom_number">物料编号</param>
        /// <returns>物料数据</returns>
        public async Task<bill_of_materials> GetBillbackfill(string? material_code)
        {
            Expression<Func<bill_of_materials, bool>> wherename = x => x.material_code == material_code;
            var list = await bill_Of_MaterialsRepository.GetShowObjAsync(wherename);
            return list;
        }

    }
}
